package com.cez.itstudio01.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cez.itstudio01.mapper.BookMapper;
import com.cez.itstudio01.pojo.Book;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * author: chenez
 * date: 2022/9/9 22:18
 * description:
 */
@Controller
public class bookController {
    @Autowired
    BookMapper bookMapper;
//    @GetMapping("/book")
//    public String book(){return "redirect:/admin/book.html";}
    /**
     * @Description 有无条件通用查询
     * @Date 2022/9/10 14:45
     * @param page
     * @param limit
     * @param map1
     *@return java.lang.Object
     **/
    @GetMapping("/admin/getBookJson")
    @ResponseBody
    public Object getBook(@RequestParam("page") Integer page,
                          @RequestParam("limit") Integer limit,
                          @RequestParam(required = false) Map map1){
            String ID=null;
            String name=null;
            String publisher=null;
            String searchParams = (String) map1.get("searchParams");
            Map maps = (Map) JSON.parse(searchParams);
            if(maps!=null) {
                ID = (String) maps.get("ID");
                name = (String) maps.get("name");
                publisher = (String) maps.get("publisher");
            }
        Page<Book> bookPage = new Page<>(page,limit);
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(ID),"ID",ID).eq(StringUtils.isNotBlank(name),"name",name).eq(StringUtils.isNotBlank(publisher),"publisher",publisher);
        Page<Book> selectPage = bookMapper.selectPage(bookPage,wrapper);
        List<Book> books = selectPage.getRecords();
        HashMap<Object, Object> map = new HashMap<>();
        Long count = bookMapper.selectCount(null);
        map.put("code",0);
        map.put("message","");
        map.put("count",count);
        map.put("data",books);
        return  map;
    }
    /**
     * @Description 单个删除
     * @Date 2022/9/10 0:35
     * @param id
     *@return java.lang.Object
     **/
    @PostMapping("/index/admin/deleteBookJson")
    @ResponseBody
    public Object deleteBook(Integer id){
        bookMapper.deleteById(id);
        HashMap<Object, Object> res = new HashMap<>();
        res.put("code",1);
        res.put("message","删除成功");
        return res;
    }
    /**
     * @Description 添加图书
     * @Date 2022/9/10 1:12
     * @param ID
     * @param name
     * @param total_num
     * @param publisher
     *@return java.lang.Object
     **/
    @PostMapping("/index/admin/addBookJson")
    @ResponseBody
    public Object addBook(String ID,String name,Integer total_num,String publisher){
        Book book = new Book(ID, name, 0, total_num, publisher);
        bookMapper.insert(book);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code",1);
        map.put("message","添加成功");
        return map;
    }
    @GetMapping("/admin/add_book")
    public String add_book(){return "/admin/add_book.html" ;}
    /**
     * @Description 批量删除
     * @Date 2022/9/10 1:14
     * @param
     *@return java.lang.Object
     **/
    @PostMapping("/index/admin/deleteAllBookJson")
    @ResponseBody
    public Object deleteBatch(@RequestParam Map post_data){
        Set key = post_data.keySet();
        ArrayList list = new ArrayList<>();
        for(Object key1:key){
           list.add(post_data.get(key1));
        }
        ArrayList arrayList=new ArrayList<>();
        for(int i=0;i<= list.size()-1;i=i+5){
            Object ID = list.get(i);
            arrayList.add(ID);
        }
        bookMapper.deleteBatchIds(arrayList);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code",1);
        map.put("message","删除成功");
        return map;
    }
    @GetMapping("/admin/edit_book")
    public String edit_book(){
        return "/admin/edit_book.html";
    }
    /**
     * @Description 编辑
     * @Date 2022/9/10 13:27
     * @param ID
     * @param name
     * @param borrowed_num
     * @param total_num
     * @param publisher
     *@return java.lang.Object
     **/
    @PostMapping("/index/admin/editBookJson")
    @ResponseBody
    public Object editBook(String ID,String name,Integer borrowed_num,Integer total_num,String publisher){
        Book book = new Book(ID, name, borrowed_num, total_num, publisher);
        System.out.println(book);
        UpdateWrapper<Book> wrapper = new UpdateWrapper<>();
        wrapper.eq("ID",ID);
        int update = bookMapper.update(book, wrapper);
        HashMap<Object, Object> map = new HashMap<>();
        if(update!=0) {
            map.put("code", "1");
            map.put("message", "更新成功");
        }
        else {
            map.put("code","0");
            map.put("message","书号不存在,更新失败");
        }
        return map;
    }
}
